<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="userObj.photo" @click="$refs.iptFile.click()" />
          <!-- file选择框 -->
          <input type="file" ref="iptFile" v-show="false" accept="image/*" @change="onFileChange" />
        </template>
      </van-cell>
      <!-- 姓名部分 -->
      <van-cell title="名称" is-link :value="userObj.name" @click="showNameDialogFn"/>
      <!-- 修改用户名称的对话框 -->
      <van-dialog
        v-model="isShowNameDialog"
        title="修改名称"
        show-cancel-button
        :before-close="onNameDialogBeforeClose"
      >
        <!-- 输入框 -->
        <van-field
          v-model.trim="userName"
          input-align="center"
          maxlength="7"
          placeholder="请输入名称"
          v-fofo
        />
      </van-dialog>
      <van-cell title="生日" is-link :value="userObj.birthday" />
    </van-cell-group>
  </div>
</template>

<script>
import { getUserProfileAPI, updatePhotoAPI, updateUserInfoAPI } from '@/api/index'
import { Notify } from 'vant'
export default {
  name: 'MyUserEdit',
  data () {
    return {
      userObj: {}, // 用户信息对象
      isShowNameDialog: false, // 是否显示姓名弹出框
      userName: '' // 编辑用户名
    }
  },
  created () {
    this.getUserProfileFn()
  },
  methods: {
    async getUserProfileFn () {
      const res = await getUserProfileAPI()
      console.log(res)
      this.userObj = res.data.data
    },
    // 文件选择
    async onFileChange (event) {
      // console.log(event.target.files[0])
      if (event.target.files.length === 0) return
      const fd = new FormData()
      fd.append('photo', event.target.files[0])
      const res = await updatePhotoAPI(fd)
      console.log(res)
      this.userObj.photo = res.data.data.photo
    },
    // 点击名字 显示弹出框
    showNameDialogFn () {
      this.isShowNameDialog = true
      this.userName = this.userObj.name // 设置默认显示内容
    },
    // 姓名修改弹出层-关闭方法
    async onNameDialogBeforeClose (action, done) {
      // action的值: confirm或cancel(点击按钮区分)
      if (action === 'confirm') {
        // 确定
        // unicode编码 \u4092
        // url编码 %E2%C3%D1
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          // 通过校验
          // 调用接口
          await updateUserInfoAPI({
            userName: this.userName
          })
          // 更新页面显示的名字
          this.userObj.name = this.userName
          // 关闭弹窗
          done()
        } else {
          // 提示用户
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else if (action === 'cancel') {
        // 取消
        done()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
